﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICFamily
    {
       public DataSet CFamily_GetList(out int ItemCount, int CandidateID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CFamilyInfo CFamily_GetInfo(int _CFamily_ID)
        {
            CFamilyInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CFamily_ID", _CFamily_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CFamilyInfo();
                    retVal.ID = Convert.ToInt32(dr["CFamily_ID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CFamily_CandidateID"]);

                    retVal.Name = Convert.ToString(dr["CFamily_Name"]);
                    retVal.DOB = dr["CFamily_DOB"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["CFamily_DOB"]);
                    retVal.Employment = Convert.ToString(dr["CFamily_Employment"]);
                    retVal.Age = dr["CFamily_Age"] == DBNull.Value ? 0 : Convert.ToInt32(dr["CFamily_Age"]);
                    retVal.Relationship = Convert.ToString(dr["CFamily_Relationship"]);
                    retVal.Address = Convert.ToString(dr["CFamily_Address"]);
                    retVal.IsDependent = dr["CFamily_IsDependent"] == DBNull.Value ? false : Convert.ToBoolean(dr["CFamily_IsDependent"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int CFamily_Insert(CFamilyInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CFamily_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CFamily_Name", eInfo.Name);
            if(eInfo.DOB!=DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CFamily_DOB", eInfo.DOB);
            else
                dbCmd.Parameters.AddWithValue("@CFamily_DOB", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@CFamily_Employment", eInfo.Employment);
            dbCmd.Parameters.AddWithValue("@CFamily_Age", eInfo.Age);
            dbCmd.Parameters.AddWithValue("@CFamily_Relationship", eInfo.Relationship);
            dbCmd.Parameters.AddWithValue("@CFamily_Address", eInfo.Address);
            dbCmd.Parameters.AddWithValue("@CFamily_IsDependent", eInfo.IsDependent);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CFamily_Update(CFamilyInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CFamily_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CFamily_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CFamily_Name", eInfo.Name);
            if (eInfo.DOB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CFamily_DOB", eInfo.DOB);
            else
                dbCmd.Parameters.AddWithValue("@CFamily_DOB", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@CFamily_Employment", eInfo.Employment);
            dbCmd.Parameters.AddWithValue("@CFamily_Age", eInfo.Age);
            dbCmd.Parameters.AddWithValue("@CFamily_Relationship", eInfo.Relationship);
            dbCmd.Parameters.AddWithValue("@CFamily_Address", eInfo.Address);
            dbCmd.Parameters.AddWithValue("@CFamily_IsDependent", eInfo.IsDependent);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CFamily_Delete(int CFamilyID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CFamily_ID", CFamilyID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CFamily_Exist(int _ID, int _CandidateID, string _No)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateFamily_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CFamily_ID", _ID);
            dbCmd.Parameters.AddWithValue("@CFamily_CandidateID", _CandidateID);
            dbCmd.Parameters.AddWithValue("@CFamily_Name", _No);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
